<?php
/**
 * Created by PhpStorm.
 * User: h
 * Date: 2021/4/26
 * Time: 15:12
 */

namespace app\api\controller\v1;


use app\common\model\CaseModel;
use app\common\model\Cate;

class Sucai extends BaseController
{
    public function caseindex()
    {
        $cate = Cate::where(['status' => 1, 'type' => 2, 'pid' => 0])->field('id,name')->limit(0, 4)->select();
        $data1 = [];
        $data2 = [];
        $data3 = [];
        $data4 = [];
        $cate1 = [];
        $cate2 = [];
        $cate3 = [];
        $cate4 = [];
        if (isset($cate[0]) && !empty($cate[0])) {
            $cate1 = $cate[0];
            $data1 = CaseModel::where('status', 1)
                ->where('cate_id', $cate[0]['id'])
                ->field('id,title,image')
                ->limit(0, 9)
                ->order('sort desc')
                ->select()->toArray();
            if ($data1) {
                $data1 = changeImage($data1, 'image');
            }
        }
        if (isset($cate[1]) && !empty($cate[1])) {
            $cate2 = $cate[1];
            $data2 = CaseModel::where('status', 1)
                ->where('cate_id', $cate[1]['id'])
                ->field('id,title,image')
                ->limit(0, 8)
                ->order('sort desc')
                ->select()->toArray();
            if ($data2) {
                $data2 = changeImage($data2, 'image');
            }
        }
        if (isset($cate[2]) && !empty($cate[2])) {
            $cate3 = $cate[2];
            $data3 = CaseModel::where('status', 1)
                ->where('cate_id', $cate[2]['id'])
                ->field('id,title,image')
                ->limit(0, 8)
                ->order('sort desc')
                ->select()->toArray();
            if ($data3) {
                $data3 = changeImage($data3, 'image');
            }
        }
        if (isset($cate[3]) && !empty($cate[3])) {
            $cate4 = $cate[3];
            $data4 = CaseModel::where('status', 1)
                ->where('cate_id', $cate[3]['id'])
                ->field('id,title,image')
                ->limit(0, 8)
                ->order('sort desc')
                ->select()->toArray();
            if ($data4) {
                $data4 = changeImage($data4, 'image');
            }
        }
        $data = [
            'cate1' => $cate1,
            'data1' => $data1,
            'cate2' => $cate2,
            'data2' => $data2,
            'cate3' => $cate3,
            'data3' => $data3,
            'cate4' => $cate4,
            'data4' => $data4,
        ];

        return sucessMsg(200, '获取成功', $data);
    }

    public function caseall()
    {

    }

    public function caselist()
    {
        $cate_id = input('cate_id');
        if (!$cate_id) {
            $cate_id = Cate::where('status', 1)->where('type', 2)->select()->toArray();
            $cate_id = array_column($cate_id, 'id');
            $where[] = ['cate_id', 'in', $cate_id];  //根据子级查询数据
            $cate_this = ['id' => 0, 'name' => '素材库'];
        } else {
            $cate_this = Cate::field('id,name')->find($cate_id);
            $where = ['cate_id' => $cate_id];
        }
        $page = input('page') ? input('page') : 1;
        $data = CaseModel::where(['status' => 1])
            ->where($where)
            ->field('id,image,title,file_path')
            ->order('sort desc')
            ->limit(($page - 1) * 15, 15)
            ->select()->toArray();
        $maxpage = CaseModel::where(['status' => 1])
            ->where($where)
            ->field('id')
            ->count();
        $maxpage = ceil($maxpage / 15);
        $cate = Cate::where(['type' => 2, 'status' => 1, 'pid' => 0])->select();
        if ($data) {
            $data = changeImage($data, 'image');
            $data = changeImage($data, 'file_path');
        }
        return sucessMsg(200, '获取成功', ['cate_this' => $cate_this, 'data' => $data, 'maxpage' => $maxpage, 'cate' => $cate]);
    }

    public function content()
    {
        $id = input('id');
        if (!$id) {
            return errMsg(400, '请传入参数');
        }
        $data = CaseModel::find($id);
        $cate = Cate::find($data->cate_id);
        $data->image = env('app.yuming') . $data->image;
        $data->param = json_decode($data->param);
        $data->cate = $cate->name;
        $pdf = $data->file_path;
        if (is_file($pdf)) {
            $data->file_size = trans_byte(filesize($pdf));
            $finfo = finfo_open(FILEINFO_MIME); // 返回 mime 类型
            $lei = finfo_file($finfo, $pdf);
            finfo_close($finfo);
            $data->file_type = getSub($lei, '/', ';');
        } else {
            $data->file_size = '0kb';
            $data->file_type = "暂无文件";
        }
        $data->file_path = env('app.yuming') . $data->file_path;

        $about_sucai = CaseModel::where(['cate_id' => $data->cate_id, 'status' => 1])
            ->where('id', '<>', $id)
            ->field('id,image,title')
            ->limit(0, 4)
            ->order('sort desc')
            ->select()->toArray();
        if ($about_sucai) {
            $about_sucai = changeImage($about_sucai, 'image');
        }
        $sucai_id = array_column($about_sucai, 'id');
        $sucai_id = array_merge($sucai_id, [intval($id)]);
        $cate = Cate::where(['status' => 1, 'type' => 2, 'pid' => 0])->field('id,name')->select()->toArray();
        $cate = array_column($cate, 'id');
        $about_tuijian = CaseModel::where(['status' => 1])
            ->where('cate_id', 'in', $cate)
            ->where('id', 'not in', $sucai_id)
            ->field('id,image,title')
            ->limit(0, 8)
            ->order('sort desc')
            ->select();
        if ($about_tuijian) {
            $about_tuijian = changeImage($about_tuijian, 'image');
        }

        return sucessMsg(200, '获取成功', ['data' => $data, 'about_sucai' => $about_sucai, 'about_tuijian' => $about_tuijian]);
    }
}